<template>
  <div class="app-container">
    <el-form :inline="true" :model="searchObj" class="demo-form-inline">
      <el-form-item label="课程名称">
        <el-input v-model="searchObj.title" placeholder="课程名称"></el-input>
      </el-form-item>
      <el-form-item label="课程状态">
        <el-select v-model="searchObj.status" placeholder="课程状态">
          <el-option label="已发布" value="Normal"></el-option>
          <el-option label="未发布" value="Draft"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="添加时间">
        <el-date-picker
          v-model="searchObj.begin"
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="searchObj.end"
          type="datetime"
          placeholder="选择截止时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getList">查询</el-button>
        <el-button type="primary" @click="cleanBtn">清空</el-button>
        <el-button type="primary" @click="getAll">全部</el-button>
      </el-form-item>

    </el-form>
    <el-table
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="序号" width="115">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column label="课程名称" width="220" align="center">
        <template slot-scope="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column label="课程讲师" width="220" align="center">
        <template slot-scope="scope">
          {{ scope.row.teacherName }}
        </template>
      </el-table-column>
      <el-table-column label="课程价格" width="220" align="center">
        <template slot-scope="scope">
          {{ scope.row.price }}
        </template>
      </el-table-column>
      <el-table-column label="状态" width="220" align="center">
        <template slot-scope="scope">
          {{ scope.row.status=='Draft'?'未发布':'已发布' }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间" width="250">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.gmtCreate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <p><el-button type="primary" @click="editBaseInfo(scope.row.id)">编辑课程基本信息</el-button></p>
          <p><el-button type="primary" @click="editChapter(scope.row.id)">编辑课程章节小节</el-button></p>
          <el-button type="primary" v-if="scope.row.status=='Draft'" @click="publishCourse(scope.row.id)">发布课程</el-button>
          <el-button type="danger" @click="remove(scope.row.id)">删除课程</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      align="center"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList" />

  </div>
</template>

<script>
  import Pagination from '@/components/Pagination'
  import course from '@/api/course'


  export default {
    components: { Pagination },
    data() {
      return {
        list: null,//分页集合
        total: 0,//总记录数
        listQuery: {//分页参数
          page: 1,
          limit: 10
        },
        searchObj: {//查询条件

        },
      }
    },
    created() {
      this.getList()
    },
    methods: {
      //查询课程列表
      getList(){
          course.pageCourse(this.listQuery.page,this.listQuery.limit,this.searchObj)
            .then(response=>{
              this.list = response.data.list
              this.total = response.data.total
            })
      },
      //查询全部
      getAll(){
        this.searchObj={}
        course.pageCourse(1,10,this.searchObj)
          .then(response=>{
            this.list = response.data.list
            this.total = response.data.total
          })
      },
      //删除
      remove(id){

      },
      //进入编辑页面
      updateTeacher(id){

      },
      //编辑课程基本信息
      editBaseInfo(id){
        this.$router.push({path:'/course/edit/'+id})
      },
      //编辑课程章节小节
      editChapter(id){
        this.$router.push({path:'/course/chapter/'+id})
      },
      //发布课程
      publishCourse(id){
        this.$router.push({path:'/course/publish/'+id})
      },
      //清空
      cleanBtn(){
        this.searchObj={}
      }
    }
  }
</script>
